package bairui.international.weixin.portal.miniapp.controller.index.module;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.querydsl.QSort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.model.uml.model.status.State_UpDown;
import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;

import bairui.international.model.domain.jpa.TB_IndexModule;
import bairui.international.model.domain.jpa.TB_IndexModuleImg;
import bairui.international.model.domain.jpa.TB_IndexModuleProduct;
import bairui.international.model.domain.jpa.TB_Product;
import bairui.international.model.type.Type_IndexModule;
import bairui.international.model.type.Type_IndexModuleImg;
import bairui.international.service.Service_IndexModule;
import bairui.international.service.Service_IndexModuleImg;
import bairui.international.service.Service_IndexModuleProduct;
import bairui.international.weixin.portal.miniapp.controller.product.product.C_Product_ShowImg_Get;
import lombok.Getter;

/**
 *
 * 模块列表
 *
 * @generated
 * 
 * @see STest_C_Index_Module_List_Get 测试
 *
 */
@Controller
public class C_Index_Module_List_Get
 extends SC_Index_Module_List_Get 
{
	
	@Autowired private Service_IndexModule service_IndexModule;
	@Autowired private Service_IndexModuleProduct service_IndexModuleProduct;
	@Autowired private Service_IndexModuleImg service_IndexModuleImg;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@GetMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request
	) throws Exception {
		List<TB_IndexModule> modules = service_IndexModule.list(
				it->it.state.eq(State_UpDown.Uped)
					.and(it.pageFlag.isFalse()),
				it->new QSort(it.orderValue.desc())
			).getContent();
		return modules
				.stream()
				.map(it->{
					Type_IndexModule type = it.getType();
					if(type == Type_IndexModule.Img){
						List<TB_IndexModuleImg> imgs = service_IndexModuleImg.list(
								it2->it2.indexModule.eq(it).and(it2.state.eq(State_UpDown.Uped)),
								it2->new QSort(it2.orderValue.desc())
							).getContent();
						return new Output(Type_IndexModule.Img, it.getName(), it.getComment(), it.getTopFlag(), imgs);
					}else if(type == Type_IndexModule.Product){
						List<TB_IndexModuleProduct> products = service_IndexModuleProduct.list(
								it2->it2.indexModule.eq(it).and(it2.state.eq(State_UpDown.Uped)),
								it2->new QSort(it2.orderValue.desc())
							).getContent();
						return new Output(Type_IndexModule.Product, it.getName(), it.getComment(), it.getTopFlag(), products);
					}else {
						throw new RuntimeException();
					}
				})
				.collect(Collectors.toList());
	}
	
	public static class Output {
		private @Getter Type_IndexModule type;
		private @Getter String name;
		private @Getter String comment;
		private @Getter boolean topFlag;
		private @Getter List<?> items = new ArrayList<>();
		public Output(Type_IndexModule type, String name, String comment, boolean topFlag, List<?> modules){
			this.type = type;
			this.name = name;
			this.comment = comment;
			this.topFlag = topFlag;
			//
			if(type == Type_IndexModule.Img){
				items = modules
						.stream()
						.map(it->new ImgModuleOutput((TB_IndexModuleImg)it))
						.collect(Collectors.toList());
			}else if(type == Type_IndexModule.Product){
				items = modules
						.stream()
						.map(it->new ProductModuleOutput((TB_IndexModuleProduct)it))
						.collect(Collectors.toList());
			}else {
				throw new RuntimeException();
			}
		}
	}

	public static class ImgModuleOutput {
		private @Getter String url;
		private @Getter String imgUrl;
		private @Getter Type_IndexModuleImg format;
		public ImgModuleOutput(TB_IndexModuleImg it) {
			this.url = it.getUrl();
			this.imgUrl = C_Index_Module_ShowModuleImg_ShowImg.ShowImg.Operate(it.getId()).getUrl();
			this.format = it.getFormat();
		}
		
	}
	
	public static class ProductModuleOutput {
		private @Getter long id;
		private @Getter String imgUrl;
		private @Getter String name;
		private @Getter String number;
		private @Getter Double price;
		private @Getter String comment;
		
		public ProductModuleOutput(TB_IndexModuleProduct it) {
			TB_Product p = it.getProduct();
			this.id = p.getId();
			this.imgUrl = C_Product_ShowImg_Get.Get.Operate(p.getId()).getUrl();
			this.name = p.getName();
			this.number = p.getNumber();
			this.price = p.getPrice();
			this.comment = it.getComment();
		}
		
	}
	
}
